<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "UserLogin".
 */
class UserLogin extends ActiveRecord {

    // @name user login client defined
    const ClientWeb = 1;
    const ClientWap = 2;
    const ClientAndroid = 3;
    const ClientIos = 4;
    public static $clientSelector = [
        self::ClientWeb => ['title' => '网页登陆', 'status' => 'blue'],
        self::ClientWap => ['title' => '手机H5', 'status' => 'blue'],
        self::ClientAndroid => ['title' => '安卓', 'status' => 'blue'],
        self::ClientIos => ['title' => '苹果', 'status' => 'blue'],
    ];

    // @name user login account type defined
    const LoginTypeAccount = 1;
    const LoginTypeQQ = 2;
    const LoginTypeWeChat = 3;
    const LoginTypeBaidu = 4;
    public static $loginTypeDesc = [
        self::LoginTypeAccount => '注册账号',
        self::LoginTypeQQ => 'QQ授权',
        self::LoginTypeWeChat => '微信授权',
        self::LoginTypeBaidu => '百度授权',
    ];
    public static $loginTypeSelector= [
        self::LoginTypeAccount => ['title' => '注册账号', 'status' => 'blue'],
        self::LoginTypeQQ => ['title' => 'QQ授权', 'status' => 'blue'],
        self::LoginTypeWeChat => ['title' => '微信授权', 'status' => 'blue'],
        self::LoginTypeBaidu => ['title' => '百度授权', 'status' => 'blue'],
    ];

    // only define rules for those attributes that
    // will receive user inputs.
    public function rules()
    {
        return [
            [['user_id', 'ip', 'token', 'login_type'], 'required'],
            [['user_id', 'client', 'login_type'], 'integer'],
            [['ip', 'token'], 'string', 'max' => 32],
            [['address'], 'string', 'max' => 64],
        ];
    }
    /**
     * @name 字段名称
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'user_id' => '用户',
            'ip' => '登陆IP',
            'token' => 'TOKEN',
            'login_type' => '登陆类型',
            'client' => '登陆设备',
            'address' => '登陆地址',
        ];
    }

    /**
     * @name 获取user信息
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['user_id' => 'id']);
    }
}